﻿
var editobj;


function converNum(code, num) {
    code = code.replace(/\[(.+?)\]/g, function (m, e1) {
        return "[" + num + "]";
    });
    return code;
}

function saveOrder() {
    $("#p_body").html("");
    var data = $("#left_body li").map(function () {
        var type = $(this).attr("litype");
        var result = 0;
        var parant = $(this).children("div");
        var Content, Title, Type, IsTemp, tempfile;
        if (type == "img") {
            Title = parant.find(".imgtitle").val();
            Content = parant.find(".imgfile").val();
            tempfile = parant.find(".tempfile").val();
            IsTemp = Boolean(parant.find(".istemp").val());
            $("#p_body").append("<img style=\"max-width:580px\" src='/img?m=MaxWidth&w=580&file=" + IsTemp ? tempfile : Content + "'/><br/><span>" + Title + "</span><br/>");
        } else if (type == "txt") {
            Content = parant.find(".txtbody").val();
            $("#p_body").append(Content + "<br/>");
        } else if (type == "mp4") {
            Content = parant.find(".mp4body").val();
            $("#p_body").append("<div style='width:580px;height:380px;'>" + Content + "</div><br/>");
        } 
        $("#left_body").children("li").each(function (xi) {
            $(this).find("input,textarea").each(function () {
                var name = $(this).attr("name");
                if (name != null && name != undefined) {
                    $(this).attr("name", converNum(name, xi));
                }
            });
        });
    });
};

function deleteeditli(objthis)
{
    $(objthis).parent().parent().parent().remove();
    saveOrder();
}

function openwordbox() {
    setmodalLoding();
    var editor;
    editobj = null;
    $("#txtcontent").val("");
    $('#writewordbox').modal({
        open: function (event, ui) {
            editor=createEditor();
        },
        beforeClose: function (event, ui) {
            if (editor) {
                editor.remove();
            }
        }
    });
}

function wordedit(objthis)
{
    setmodalLoding();
    var editor;
    editobj = $(objthis).parent().parent();
    var tbody = editobj.children(".txtbody").val();
    $("#txtcontent").val(tbody);
    $('#writewordbox').modal({
        open: function (event, ui) {
            editor=createEditor();
        },
        beforeClose: function (event, ui) {
            if (editor) {
                editor.remove();
            }
        }
    });
    editor.sync();
}

function SaveWord()
{
    var tbody = $("#txtcontent").val();
    var jjbody = tbody.trim().replace(/&nbsp;/g, "").replace(/<(.+?)>/g, "").substring(0, 20);
    if (editobj) {
        editobj.children(".txtbody").val(tbody);
        editobj.children("p").text(jjbody);
    } else {
        $("#left_body").append($("#templatenotetext").html().fill({Content:tbody,body:jjbody}));
    }
    saveOrder();
    closemodal();
}


function openAddtypebox()
{
    showmsgmodalcontent($("#templatetype_add").html());
}

function dosavetype(objthis)
{
    var name=$(objthis).siblings("input").eq(0).val();
    $.getJSON("/action/Article/AddType?jsoncallback=?", { name: name }, function (data) {
        if (data.error == 0) {
            if (data.data)
            {
                var html="";
                $.each(data.data, function (i, n) {
                    html += "<option value='"+n.Id+"'>"+n.Name+"</option>";
                })
                $("#seltype").html(html);
            }
            ClosemsgModal();
        } else {
            alert(data.msg);
        }
    });
    
}

function createUploader(dofn,multiple) {
    var images = new Array();
    var count = 0;
    var uploader = new qq.FineUploader({
        element: document.getElementById('bootstrapped-fine-uploader'),
        request: {
            endpoint: '/Action/Picture/Upload'
        },
        text: {
            uploadButton: '<i class="icon-upload icon-white"></i> 请选择上传的文件',
            retryButton: '<i class="icon-upload icon-trash"></i> '
        },
        template: '<div class="qq-uploader span12">' +
                    '<pre class="qq-upload-drop-area span12"><span>{dragZoneText}</span></pre>' +
                    '<div class="qq-upload-button btn btn-success" style="width: auto;">{uploadButtonText}</div>' +
                    '<ul class="qq-upload-list" style="margin-top: 10px; text-align: center;"></ul>' +
                  '</div>',
        classes: {
            success: 'alert alert-success',
            fail: 'alert alert-error'
        },
        debug: true,
        multiple: multiple,
        callbacks: {
            onComplete: function (id, filename, result) {
                images[images.length] = result.name;
                count--;
                if (count == 0) {
                    //$("#files").val(images.join(","));
                    if (dofn) {
                        dofn(images);
                    }
                }
            },
            onSubmit: function (id, fileName) {
                count++;
            },
            onError: function (id, fileName, reason) {
                count--;
                if (count == 0) {
                    //$("#files").val(images.join(","));
                    if (dofn) {
                        dofn(images);
                    }
                }
            },
            onUpload: function (id, fileName, xhr) { }
        }
    });
}

function setucentmenu(tag)
{
    $("#ucentermenu [tag=" + tag + "]").addClass("active").siblings().removeClass("active");
}